const router = require('koa-router')()
const dbs = require('../../mysql/index').isconnect()

router.get('/all', async ctx => {
  ctx.body = await dbs.find('*', 'superlink')
})

router.post('/add', async ctx => {
  let { linkname, linkurl } = ctx.request.body
  // console.log('test', ctx.request.body, linkname, linkurl)
  if (Object.keys(ctx.request.body).length == 0) {
    ctx.body = { code: 10000, result: '请输入链接名和地址' }
  } else if (linkname === undefined || linkurl === undefined) {
    ctx.body = { code: 10000, result: '请输入完整的数据' }
  } else if (linkname === '' || linkurl === '') {
    ctx.body = { code: 10000, result: '链接名或地址为空' }
  } else {
    let res = await dbs.insert(
      'superlink',
      `('${linkurl}','${linkurl}')`,
      'backstage'
    )
    if (res.code == 1) {
      ctx.body = { code: 200, result: '请求成功' }
    } else {
      ctx.body = { code: 10000, result: '请求失败，请检查数据' }
    }
  }
})

module.exports = router.routes()
